<?php
include_once('functions.php');

// 设置宽度
$width = 80;

// 设置高度
$height = 34;

// 创建画布
$img = imagecreatetruecolor($width,$height);

// 设置背景颜色,颜色必须使用rgb格式
$bgColor = imagecolorallocate($img,238,238,238);

// 把背景色加入画布
imagefilledrectangle($img,0,0,$width,$height,$bgColor);

// 设置字体颜色,颜色必须使用rgb格式
$textColor = imagecolorallocate($img,255,0,0);

$code_1 = build_ranstr(1);
$code_2 = build_ranstr(1);
$code_3 = build_ranstr(1);
$code_4 = build_ranstr(1);

// 设置字体路径
$fontPath = 'C:\Users\86132\Desktop\php_pro\k2222student\assets\fonts\OpenSans-Light.ttf';

// 把文字加入画布
imagettftext($img,16,mt_rand(-30,30),1,26,$textColor,$fontPath,$code_1);
imagettftext($img,16,mt_rand(-30,30),20,26,$textColor,$fontPath,$code_2);
imagettftext($img,16,mt_rand(-30,30),40,26,$textColor,$fontPath,$code_3);
imagettftext($img,16,mt_rand(-30,30),60,26,$textColor,$fontPath,$code_4);

// 绘制一些点状像素
for($i = 0;$i < 15;$i++)
{
    imagesetpixel($img,mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
}

// 绘制一些线像素
for($i = 0;$i < 5;$i++)
{
    imageline($img,mt_rand(0,$width),mt_rand(0,$height),mt_rand(0,$width),mt_rand(0,$height),imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255)));
}

// 开启session会话
session_start();

// 注册会话变量
$_SESSION['verify'] = $code_1.$code_2.$code_3.$code_4;

// 设置头部信息
header('Content-Type:image/png');

// 输出图片
imagepng($img);